<?php

function p($data){
  echo '<pre>';
  print_r($data);
  echo '</pre>';
}


/**
 * 官网 http://code.google.com/p/phpquery/
 * 爬知乎PHP专栏每条帖子(后期输入专栏爬)
 */
include 'phpQuery/phpQuery.php';
header("Content-Type:text/html;charset=utf-8");
phpQuery::newDocumentFile('https://www.zhihu.com/search?type=content&q=php');
$artlist = pq(".title");
$praise = pq(".zm-item-vote-count");
$author = pq(".author-line");

//不知为何要严格定义这个数组,否则无法赋值
$data = array();
foreach($artlist as $k=>$li){
  $data['title']["$k"] = pq($li)->html();
}
$even = 0;
foreach ($praise as $k => $v) {
  if ($k%2!=0) {
    continue;
  }
  $data['praise']["$even"] = pq($v)->html();
  $even++;
}

foreach ($author as $k => $v) {
  if (pq($v)->html() == '匿名用户') {
    $data['author']["$k"] = '匿名用户';
  }else{
    $data['author']["$k"] = pq($v)->find('span')->find('a')->html();
  }
  // $data['author']["$k"] = pq($v)->html();
}
p($data);

/**
 * 解析XML文档
 */
// include 'phpQuery/phpQuery.php';
// phpQuery::newDocumentFile('test.xml');
// echo pq('contact > age:eq(0)');
?>
